package com.tenton.service.Impl;

import com.tenton.mapper.UserMapper;
import com.tenton.pojo.User;
import com.tenton.service.UserService;
import com.tenton.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Map;

/**
 * @Date: 2021/1/13
 * @Author: Tenton
 * @Description:
 */
public class UserServiceImpl implements UserService {
    /**
     * 添加用户
     * @param user
     * @return
     */
    @Override
    public int insertUser(User user) {
        SqlSession sqlSession = MybatisUtils.getSqlSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int result = mapper.insertUser(user);
        return result;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @Override
    public int deleteUser(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int result = mapper.deleteUser(id);
        return result;
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @Override
    public boolean updateUser(User user) {
        SqlSession sqlSession = MybatisUtils.getSqlSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        boolean b = mapper.updateUser(user);
        return b;
    }

    /**
     * 查询，用于验证MySQL表中是否拥有该用户
     * @param userName
     * @param password
     * @return
     */
    @Override
    public User getUser(String userName, String password) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUser(userName, password);
        return user;
    }

    /**
     * 查询所有用户信息
     * @return
     */
    @Override
    public List<User> listUser() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.listUser();
        return users;
    }

    /**
     * 通过用户名查询用户
     * @param userName
     * @return
     */
    @Override
    public List<User> listUserByUserName(String userName) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.listUserByUserName(userName);
        return users;
    }

    /**
     * 通过用户名查询用户
     * @param id
     * @return
     */
    @Override
    public User getUserById(int id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserById(id);
        return user;
    }

    /**
     * 添加日志到redis
     * @param username
     * @param time
     * @param action
     */
    @Override
    public void addRedisLog(String username, String time, String action) {
        //创建redis连接
        Jedis jedis = new Jedis("localhost",6379);
        //用户名+时间+登录
        jedis.hset(username,time,action);
        //关闭连接
        jedis.close();
    }

    @Override
    public Map<String, String> listRedisLog(String username) {
        //创建redis连接
        Jedis jedis = new Jedis("localhost",6379);
        Map<String, String> stringStringMap = jedis.hgetAll(username);
        return stringStringMap;
    }
}
